package com.xrj4j.k_tally.navigation

import androidx.compose.foundation.layout.paddingFromBaseline
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import com.xrj4j.k_tally.LocalNavController
import com.xrj4j.k_tally.ui.theme.Purple40
import com.xrj4j.k_tally.ui.theme.PurpleGrey40
import com.xrj4j.k_tally.ui.theme.PurpleGrey80
import com.xrj4j.k_tally.view.BillScreen
import com.xrj4j.k_tally.view.EditScreen
import com.xrj4j.k_tally.view.HomeScreen
import com.xrj4j.k_tally.view.SettingScreen


/**
 * 导航栏项枚举
 */
enum class NavScreen(
    val title: String,
    val route: String,
    val icon: ImageVector,
    val content:@Composable () -> Unit
) {
    HOME("首页", "home", Icons.Default.Home,{ HomeScreen()}),
    EDIT("记一笔", "edit", Icons.Default.Edit,{ EditScreen() }),
    BILL("账单", "bill", Icons.Default.DateRange,{ BillScreen()}),
    SETTING("设置", "setting", Icons.Default.Settings,{ SettingScreen() });

    companion object {
        val DEFAULT = HOME
    }
}


@Composable
fun BottomNav(
    modifier: Modifier = Modifier
) {
    val navController = LocalNavController.current
    val backStackEntry by navController.currentBackStackEntryAsState()
    NavigationBar(
        containerColor = MaterialTheme.colorScheme.surfaceVariant,
        tonalElevation = 16.dp,
        modifier = modifier,
    ) {
        NavScreen.values().forEach {
            NavigationBarItem(
                selected = getNav(backStackEntry) == it,
                onClick = {
                    navController.navigate(it.route)
                },
                icon = {
                    Icon(imageVector = it.icon, contentDescription = null)
                },
                label = {
                    Text(
                        text = it.title,
                        modifier = Modifier.paddingFromBaseline(30.dp),
                        style = MaterialTheme.typography.titleSmall
                    )
                },
                colors = NavigationBarItemDefaults.colors(
                    selectedIconColor = PurpleGrey80,
                    selectedTextColor = Purple40,
                    indicatorColor = PurpleGrey40
                )
            )
        }
    }
}

private fun getNav(backStackEntry: NavBackStackEntry?): NavScreen {
    val route = backStackEntry?.destination?.route
    if (route != null) {
        for (screen in NavScreen.values()) {
            if (route.startsWith(screen.route)) return screen
        }
    }
    return NavScreen.DEFAULT
}

@Composable
fun TallyNavHost(
    modifier: Modifier = Modifier
) {
    val navController = LocalNavController.current as NavHostController
    NavHost(
        navController = navController,
        startDestination = NavScreen.DEFAULT.route,
        modifier = modifier
    ) {
        NavScreen.values().forEach { screen ->
            composable(screen.route) {
                screen.content()
            }
        }
    }

}



